package com.zycx.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:  String  Map 转换工具
 * Created by zycx on 2018-12-07 20:29
 * Created with IntelliJ IDEA.
 */
@Slf4j
public class StringMapUtils {

    /**  String 对象进行转换
     * @param str     String 对象
     * @param symbol  Key 与 Value 之间的连接符号
     * @param connect 每个 Key_Value 之间的连接符号
     * @return countryMap
     */
    public static Map<String, Object> stringToMap(String str, String symbol, String connect) {
        //判断str是否有值
        if (null == str || "".equals(str)) {
            log.info("Before StringToMap: This string is empty !");
            return null;
        }
        log.info("Before StringToMap: " + str);
        Map<String, Object> countryMap = new HashMap<String, Object>();
        String cms = str.replace("{", "").replace("}", "").replace(" ", "");
        String[] countryMapStr = cms.split(symbol);
        for (String s : countryMapStr) {
            log.info("s:" + s);
            String[] ms = s.split(connect);
            countryMap.put(ms[0], ms[1]);
        }
        return countryMap;
    }

    /**
     * 只传 String 对象进行转换
     * @param str String 对象
     * @return countryMap
     */
    public static Map<String, String> stringToMap(String str) {
        //判断str是否有值
        if (null == str || "".equals(str)) {
            log.info("Before StringToMap(): This string is empty !");
            return null;
        }
        Map<String, String> countryMap = new HashMap<String, String>();
        String cms = str.replace("{", "").replace("}", "").replace(" ", "");
        String[] countryMapStr = cms.split(",");
        for (String s : countryMapStr) {
            String[] ms = s.split("=");
            countryMap.put(ms[0], ms[1]);
        }
        return countryMap;
    }

}
